package org.bluesky.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.bluesky.framework.ResponseResult;
import org.bluesky.framework.constants.SystemConstants;
import org.bluesky.framework.dto.LinkDto;
import org.bluesky.framework.entity.Link;
import org.bluesky.framework.mapper.LinkMapper;
import org.bluesky.framework.utils.BeanCopyUtils;
import org.bluesky.framework.service.LinkService;
import org.bluesky.framework.vo.LinkVo;
import org.bluesky.framework.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 友链(SgLink)表服务实现类
 *
 * @author makejava
 * @since 2023-02-13 15:42:56
 */
@Service("LinkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {
    @Override
    public ResponseResult allLink() {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        //审核通过link
        queryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);
        List<Link> linkList = list(queryWrapper);
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(linkList, LinkVo.class);
        return ResponseResult.okResult(linkVos);
    }

    @Override
    public PageVo getLinkList(LinkDto linkDto, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasText(linkDto.getName())){
            queryWrapper.eq(Link::getName,linkDto.getName());
        }
        if(StringUtils.hasText(linkDto.getStatus())){
            queryWrapper.eq(Link::getStatus,linkDto.getStatus());
        }
        Page<Link> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(page.getRecords(), LinkVo.class);
        PageVo pageVo = new PageVo(linkVos,page.getTotal());
        return pageVo;
    }
}

